﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLayer.Entity;
namespace DataAccessLayer.Adapter
{
    public class NhanVienAdapter
    {
        DataTable dt;
        clsKetNoiCSDL kn = new clsKetNoiCSDL();
        public DataTable LoadNV()
        {
            dt = kn.LoadDL("tbNhanVien_selectAll");
            return dt;
        }
        public void ThemNV(clsNhanVien clsNhanVien)
        {
            int Npa = 3;
            string []name=new string[Npa];
            object []value=new object[Npa];
            string sql = "tbNhanVien_Insert";
            name[0] = "@MaNV"; value[0] = clsNhanVien.MaNV;
            name[1] = "@TenNV"; value[1] = clsNhanVien.TenNV;
            name[2] = "@Active"; value[2] = clsNhanVien.Active;
            kn.UpdateDL(sql,name,value,Npa);
        }
        public void SuaNV(clsNhanVien clsNhanVien)
        {
            int Npa = 2;
            string[] name = new string[Npa];
            object[] value = new object[Npa];
            string sql = "tbNhanVien_Update";
            name[0] = "@MaNV"; value[0] = clsNhanVien.MaNV;
            name[1] = "@TenNV"; value[1] = clsNhanVien.TenNV;
            //name[2] = "@Active"; value[2] = clsNhanVien.Active;
            kn.UpdateDL(sql, name, value, Npa);
        }

        public void XoaNV(clsNhanVien clsNhanVien)
        {
            int Npa = 1;
            string[] name = new string[Npa];
            object[] value = new object[Npa];
            string sql = "tbNhanVien_Delete";
            name[0] = "@MaNV"; value[0] = clsNhanVien.MaNV;
            //name[1] = "@MaNV"; value[1] = clsNhanVien.TenNV;
            //name[2] = "@MaNV"; value[2] = clsNhanVien.Active;
            kn.UpdateDL(sql, name, value, Npa);
        }
        //mã tự tăng
        string TenNV="";
        public string MaTuTang()
        {
            dt = kn.LoadDL("tbNhanVien_selectAll");
            string Year = DateTime.Now.Year.ToString().Substring(2, 2);
            TenNV = "NV";
            if (dt.Rows.Count <= 0)
            {
                TenNV = "NV1400001";
            }
            else
            {               
                //TenNV = "NV";
                int k;
                k = Convert.ToInt32(dt.Rows[dt.Rows.Count-1][0].ToString().Substring(4,5));
                
                TenNV += Year;
                k = k + 1;
                if (k < 10)
                {
                    TenNV += "0000";
                }
                else if (k < 100)
                {
                    TenNV += "000";
                }
                else if (k < 1000)
                {
                    TenNV += "00";
                }
                else if (k < 10000)
                {
                    TenNV += "0";
                }
                TenNV += k.ToString();
            }
            return TenNV;
        }
    }
}
